#include <fstream>
#include "LogManager.h"
#ifdef _WIN32
#include <log4cpp\PropertyConfigurator.hh>
#include <log4cpp\BasicConfigurator.hh>
#else
#include <log4cpp/PropertyConfigurator.hh>
#include <log4cpp/BasicConfigurator.hh>
#endif

namespace Server
{
	// initialize static instance
	std::shared_ptr<LogManager> LogManager::m_pInstance = nullptr;

	/******************************************************************
	********
	* ++
	* Author:    Manh Pham
	* Module Name  :  LogManager.cpp
	*
	* Description  : get instance of logging class
	* Mod. History :  22/10/2014 - Manh Pham
	*                            File first created
	*
	*
	* --
	*******************************************************************
	*****/
	std::shared_ptr<LogManager> LogManager::getInstance()
	{
		if(m_pInstance == nullptr)
		{
			// create new instance if not initialize yet
			m_pInstance = std::make_shared<LogManager>(LogManager());
		}

		return m_pInstance;
	}

	/******************************************************************
	********
	* ++
	* Author:    Manh Pham
	* Module Name  :  LogManager.cpp
	*
	* Description  : Init logging service , load configuration file
	* Mod. History :  22/10/2014 - Manh Pham
	*                            File first created
	*
	*
	* --

	*******************************************************************
	*****/
	LogManager::LogManager()
	{
		// call configuration to load conbfiguration from file
		try{
//			std::ifstream fin("log4cpp.properties" , std::ios::in);
//			if(!fin.is_open())
//			{
//				throw log4cpp::ConfigureFailure("Fail to open file");
//			}
//
//			log4cpp::PropertyConfigurator::configureFromStream(fin);
//			fin.close();
            log4cpp::PropertyConfigurator::configure("log4cpp.properties");
		}
		catch(log4cpp::ConfigureFailure&)
		{
			std::ofstream fout ("log4cpp.properties1" , std::ios::out);
			fout << "# log4cpp.properties" << std::endl
				<< "log4cpp.rootCategory=DEBUG, rootAppender" << std::endl
				<< "log4cpp.appender.rootAppender=RollingFileAppender" << std::endl
				<< "log4cpp.appender.rootAppender.layout=PatternLayout" << std::endl
				<< "log4cpp.appender.rootAppender.layout.ConversionPattern=%d [%p] %m%n" << std::endl
				<< "log4cpp.appender.rootAppender.fileName=.\\RoiTrackingLog.log" << std::endl
				<< "log4cpp.appender.rootAppender.maxFileSize=11457280" << std::endl;
			fout .close();
			log4cpp::PropertyConfigurator::configure("log4cpp.properties1");
		}

	}

	/******************************************************************
	********
	* ++
	* Author:    Manh Pham
	* Module Name  :  LogManager.cpp
	*
	* Description  : get logger (root category)
	* Mod. History :  22/10/2014 - Manh Pham
	*                            File first created
	*
	*
	* --

	*******************************************************************
	*****/
	log4cpp::Category& LogManager::getLogger()
	{
		return  log4cpp::Category::getRoot();
	}

	/******************************************************************
	********
	* ++
	* Author:    Manh Pham
	* Module Name  :  LogManager.cpp
	*
	* Description  : close logging service
	* Mod. History :  22/12/2014 - Manh Pham
	*                            File first created
	*
	*
	* --
	*******************************************************************
	*****/
	void LogManager::shutdown()
	{
		log4cpp::Category::shutdown();
	}
}
